import { VEFace, VELoop, VEVertex } from "./core";

export class VEMeshExample {

  /**
   * 遍历一个面片结构的vertex、edge、loop、uv等元素
   * @param face 面元素
   */
  public loopFace(face: VEFace) {
    const first_loop: VELoop = face.first_loop;
    let iter_loop: VELoop = first_loop;
    // 循环
    do {
      // vertex顶点
      console.warn(iter_loop.vertex);
      // edge唯一边
      console.warn(iter_loop.edge);
      // loop半边结构
      console.warn(iter_loop);
      // uv数据
      console.warn(iter_loop.uv);

      // 指向下一个loop结构
      iter_loop = iter_loop.next;
    } while (first_loop !== iter_loop)
  }

  /**
   * 获取与某一个顶点为首顶点的所有loops
   * @param vertex 顶点元素
   * @returns 
   */
  public getLoopsFromVertex(vertex: VEVertex): VELoop[] {
    const loops: VELoop[] = [];
    if (vertex.loop) {
      const first_loop: VELoop = vertex.loop;
      let iter_loop: VELoop = first_loop;
      // 循环
      do {
        iter_loop = iter_loop.prev.pair; // 逆时针方向
        // 或者 顺时针方向
        // iter_loop = iter_loop.pair.next;
        loops.push(iter_loop);
      } while (first_loop !== iter_loop)
    }
    return loops;
  }

  /**
   * 获取与某一个顶点所有相邻的顶点，不包含自身
   * @param vertex 顶点元素
   * @returns 
   */
  public getAdjacentVertsFromVertex(vertex: VEVertex): VEVertex[] {
    const vertices: VEVertex[] = [];
    const loops: VELoop[] = this.getLoopsFromVertex(vertex);
    for (let i = 0; i < loops.length; i++) {
      vertices.push(loops[i].pair.vertex);
    }
    return vertices;
  }

  /**
   * 获取包含某个顶点的所有面片
   * @param vertex 顶点元素
   * @returns 
   */
  public getFacesFromVertex(vertex: VEVertex): VEFace[] {
    const faces: VEFace[] = [];
    const loops: VELoop[] = this.getLoopsFromVertex(vertex);
    for (let i = 0; i < loops.length; i++) {
      if (loops[i].face) {
        faces.push(loops[i].face!);
      }
    }
    return faces;
  }


  /**
   * 获取包含某条loop半边及对偶半边的所有面片元素
   * @param loop loop半边元素
   * @returns 
   */
  public getFacesFromLoop(loop: VELoop): VEFace[] {
    const faces: VEFace[] = [];
    if (loop.face) {
      faces.push(loop.face);
    }
    if (loop.pair.face) {
      faces.push(loop.pair.face);
    }
    return faces;
  }

  /**
   * 获取与某一面相邻的所有面片，不包含自身
   * @param face 面元素
   * @returns 
   */
  public getAdjacentFacesFromFace(face: VEFace): VEFace[] {
    const faces: VEFace[] = [];

    const first_loop: VELoop = face.first_loop;
    let iter_loop: VELoop = first_loop;
    // 循环
    do {
      if (iter_loop.pair && iter_loop.pair.face) {
        faces.push(iter_loop.pair.face);
      }
      // 指向下一个loop结构
      iter_loop = iter_loop.next;
    } while (first_loop !== iter_loop)
    return faces;
  }

}